// .cpp : Defines the entry point for the console application.
//
/*            
*/
#include "stdafx.h"
#include "stdio.h"
#include "string.h"
#include "math.h"
#define _CRT_SECURE_NO_WARNINGS 1
//string.h
class point{
public:
	double x,y,f;
//	point *next;
	void init(double x1,double y1,double f1){
		x=x1;
		y=y1;
		f=f1;
	}
	point(){
//		next=NULL;
	}
}*uk;
int n_x,n_y;
float F(float x, float y){
	return (25*y*sin(x)*sin(x)+15*(x+2)*cos(2*y+1)*cos(2*y+1)+2*x*y);
		//256-(5*(x-3)*(x-3)*sin(0.75*y-1)*sin(0.75*y-1)+5*(x-2)*(x-2)*(x+1)*cos(0.75*y)*cos(0.75*y)+50*sin(y)*sin(y)+x*y*2);
		//25*x*sin(y)*sin(y)+20*y*sin(1.5*x)*sin(1.5*x)+x*y*2;
		//5*(x-3)*(x-3)*sin(y)*sin(y)+5*(x+2)*(x+1)*cos(0.75*y)*cos(0.75*y)+50*sin(y)*sin(y);
		//25*y*sin(x)*sin(x)+15*x*cos(0.25*y)*cos(0.25*y);
		//x*sin(y)+0.5*y*sin(2*x) ;
}
double interpolate(double x,double y,point *uk,int n_x,int n_y){
	double h_x,h_y;
	h_x=uk[1].x-uk[0].x;
	h_y=uk[n_y].y-uk[0].y;
	int n,m;
	n=int(floor((x-uk[0].x)/h_x));
	m=int(floor((y-uk[0].y)/h_y));
	if((n<0)||(n>n_x)||(m<0)||(m>n_y)) return 0;
	if (n==n_x-1) n--;
	if (m==n_y-1) m--;
//	printf("float n:   %lf\nfloat m:   %lf\n",floor(3.5),floor(1.1));//(x-uk[0].x)/h_x-1,floor((y-uk[0].y)/h_y)-1);
	double x_n,y_m;
	x_n=uk[n].x;
	y_m=uk[m*n_y].y;
//	printf("x_m:   %lf\ny_m:   %lf\n",x_n,y_m);
//	printf("n:   %d\nm:   %d\n",n,m);
	return	uk[n+m*n_y].f*(x_n+h_x-x)*(y_m+h_y-y)/h_x/h_y +
			uk[n+1+m*n_y].f*(x-x_n)*(y_m+h_y-y)/h_x/h_y	+
			uk[n+(m+1)*n_y].f*(x_n+h_x-x)*(y-y_m)/h_x/h_y  +
			uk[n+1+(m+1)*n_y].f*(x-x_n)*(y-y_m)/h_x/h_y;
}
int Load_Data(){
	printf("For load from file data.txt input 0 or input filename\n");
	char s[20];
	char name[9]="data.txt";
	bool flag=1;
	FILE *f;
//	name="data.txt";
	scanf("%s",&s);
	if (s[0]!='0') flag=0;
	if (flag) if (!fopen_s(&f,name,"r")) printf("file open\n"); 
			else {printf("%s file don't exist\n",name);return -1;}
	else if (!fopen_s(&f,s,"r")) printf("file open\n");
	else { printf("%s file don't exist\n",s); return -1;}
	double p1,p2,p3;
	fscanf(f,"%d%d",&n_x,&n_y);
	uk=new point [n_x*n_y];
	int i=0; int j=0;
	while (!feof(f)){
		fscanf(f,"%lf%lf%lf",&p1,&p2,&p3);
		uk[i+j*n_y].init(p1,p2,p3);
		i++;
		if (i==n_x) {
			i=0; j++;
			if (j==n_y) break;
		}
		
	}
	fclose(f);
	return 1;
}
void Create_data(int n1,int n2,float h){
	FILE *f;
	fopen_s(&f,"data.txt","w");
	fprintf(f,"%d %d\n",n1,n2);
	for (int j=0; j<n2;j++){
		for (int i=0;i<n1;i++)
			fprintf(f,"%f %f %f\n",i*h,j*h,F(i*h,j*h));
	}
	fclose(f);
}
void Save_data(){
	FILE *f;
	fopen_s(&f,"test.txt","w");
	fprintf(f,"%d %d\n",n_x*10-9,n_y*10-9);
	float h;
	h=uk[1].x-uk[0].x;
	h=h/10.0;
	printf("h   %lf\n",h);
	for (int j=0; j<(n_y-1)*10+1;j++){
		for (int i=0;i<(n_x-1)*10+1;i++)
			fprintf(f,"%f %f %f %f\n",i*h,j*h,F(i*h,j*h),(float)interpolate(i*h,j*h,uk,n_x,n_y));//i*h,j*h,i*h*sin(j*h)+0.5*j*h*sin(2*i*h));
	}
	fclose(f);
}

int _tmain(int argc, _TCHAR* argv[])
{
char c;
Create_data(21,21,0.25);
if(Load_Data()!=-1){
	double x,y;
	x=0.4;
	y=4.1;
	//printf("x:  %lf\ny:   %lf\nans:   %lf\nfunc:   %lf",x,y,interpolate(x,y,uk,n_x,n_y),x*sin(y)+0.5*y*sin(2*x));
}

//Create_data(21,21,0.25);
Save_data();
	scanf("%c",&c);
	scanf("%c",&c);
	delete []uk;
	return 0;
}

